<?php

namespace liketp\exception;

use Exception as NativeException;

class Exception extends NativeException
{
    protected $type;

    public function __construct(string $message = '', int $code = 0)
    {
        parent::__construct($message, $code);
        $this->type = static::class;
    }

    public function getType($full = false)
    {
        $fullName = $this->type;
        $offset = strrpos($fullName, '\\');
        $offset = $offset === false ? 0 : $offset + 1;
        $pureName = substr($fullName, $offset);

        return $full ? $fullName : $pureName;
    }

    public function setType(string $type)
    {
        $this->type = $type;
        return $this;
    }

    public function __toString()
    {
        return $this->getString();
    }

    public function getString()
    {
        return json_encode($this->getData());
    }

    public function getData()
    {
        return [
            'type' => $this->getType(),
            'code' => $this->getCode(),
            'message' => $this->getMessage(),
            'file' => $this->getFile(),
            'line' => $this->getLine()
        ];
    }
}
